package xdu.ble.user.utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
import java.util.Arrays;
import java.util.UUID;

/**
 * @Author com.alibaba.lizhen
 * @Date 2021/1/14 4:01 下午
 * @Version 1.0
 * @Description
 */
public class CodecUtils {


    /**
     * MD5加密
     */
    public static String md5Hex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.md5Hex(salt + DigestUtils.md5Hex(data));
    }

    /**
     * SHA512加密
     */
    public static String shaHex(String data, String salt) {
        if (StringUtils.isBlank(salt)) {
            salt = data.hashCode() + "";
        }
        return DigestUtils.sha512Hex(salt + DigestUtils.sha512Hex(data));
    }

    /**
     * 随机生成盐
     */
    public static String generateSalt() {
        //随机4位
        return StringUtils.replace(UUID.randomUUID().toString(), "-", "").substring(0, 4);
    }

}
